package com.xkcoding.orm.mybatis.entity;


import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;

/**
 * @ClassName DsRwMybatisConfig
 * @Description TODO
 * @Author zhangweijie
 * @Date 2022/7/4 22:09
 * @ModifyDate 2022/7/4 22:09
 * @Version 1.0
 */
@Configuration
@MapperScan(basePackages = "com.bestpay.malltradeprod.dal.mapper", sqlSessionFactoryRef = "dbwrSqlSessionFactory")
public class DsRwMybatisConfig {

    @Primary // 表示这个数据源是默认数据源, 这个注解必须要加，因为不加的话spring将分不清楚那个为主数据源（默认数据源）
    @Bean("dbwrDataSource")
    @ConfigurationProperties(prefix = "spring.datasource") //读取application.yml中的配置参数映射成为一个对象
    public DataSource getdbwrDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Primary
    @Bean("dbwrSqlSessionFactory")
    public SqlSessionFactory dbwrSqlSessionFactory(@Qualifier("dbwrDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        // mapper的xml形式文件位置必须要配置，不然将报错：no statement （这种错误也可能是mapper的xml中，namespace与项目的路径不一致导致）
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/*.xml"));
        return bean.getObject();
    }

    @Primary
    @Bean("dbwrSqlSessionTemplate")
    public SqlSessionTemplate dbwrSqlSessionTemplate(@Qualifier("dbwrSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

    @Primary
    @Bean(name = "transactionManager")
    public PlatformTransactionManager transactionManager(@Qualifier("dbwrDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }
}


